package com.listeningframework.boot.codegen.context;

import com.listeningframework.boot.codegen.constants.Templates;
import org.apache.commons.lang3.StringUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Entity Context
 */
public class EntityContext {
    /**
     * 是否有效
     */
    private boolean enabled;
    /**
     * 模块子包
     */
    private String subPackage;
    /**
     * 数据表名
     */
    private String table;
    /**
     * 功能注释
     */
    private String desc;
    /**
     * 模型前缀
     */
    private String prefix;
    /**
     * 模型后缀 默认Entity
     */
    private String suffix = "Entity";
    /**
     * 是否覆盖已存在
     */
    private boolean override;
    /**
     * 是否生成Controller
     */
    private boolean withController;
    /**
     * 是否自定义Controller模板
     */
    private boolean customControllerTemplate;
    /**
     * 自定义Controller模板名称
     */
    private String customControllerTemplateName;
    /**
     * 是否生成Service
     */
    private boolean withService;
    /**
     * 是否自定义Service模板
     */
    private boolean customServiceTemplate;
    /**
     * 自定义Service模板名称
     */
    private String customServiceTemplateName;
    /**
     * 是否生成Controller
     */
    private boolean withDomain;
    /**
     * 是否自定义Domain模板
     */
    private boolean customDomainTemplate;
    /**
     * 自定义Domain模板名称
     */
    private String customDomainTemplateName;
    /**
     * 是否生成Controller
     */
    private boolean withEntity;
    /**
     * 是否忽略创建信息（id, CreatedBy,CreatedDate,UpdatedBy,UpdatedData）
     */
    private boolean withCreateInfo;
    /**
     * 是否自定义Entity模板
     */
    private boolean customEntityTemplate;
    /**
     * 自定义Entity模板名称
     */
    private String customEntityTemplateName;
    /**
     * 是否生成Mybatis数据脚本
     */
    private boolean withMybatis;
    /**
     * 是否生成Dao
     */
    private boolean withDao;
    /**
     * 是否自定义Dao模板
     */
    private boolean customDaoTemplate;
    /**
     * 自定义Dao模板名称
     */
    private String customDaoTemplateName;
    /**
     * 表字段属性
     */
    private List<TableMetaData> tableMetaDataList;

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public String getSubPackage() {
        return subPackage;
    }

    public void setSubPackage(String subPackage) {
        this.subPackage = subPackage;
    }

    public String getTable() {
        return table;
    }

    public void setTable(String table) {
        this.table = table;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public boolean isOverride() {
        return override;
    }

    public void setOverride(boolean override) {
        this.override = override;
    }

    public boolean isWithController() {
        return withController;
    }

    public void setWithController(boolean withController) {
        this.withController = withController;
    }

    public boolean isWithService() {
        return withService;
    }

    public void setWithService(boolean withService) {
        this.withService = withService;
    }

    public boolean isWithDomain() {
        return withDomain;
    }

    public void setWithDomain(boolean withDomain) {
        this.withDomain = withDomain;
    }

    public boolean isWithEntity() {
        return withEntity;
    }

    public void setWithEntity(boolean withEntity) {
        this.withEntity = withEntity;
    }

    public boolean isWithMybatis() {
        return withMybatis;
    }

    public void setWithMybatis(boolean withMybatis) {
        this.withMybatis = withMybatis;
    }

    public boolean isWithDao() {
        return withDao;
    }

    public void setWithDao(boolean withDao) {
        this.withDao = withDao;
    }

    public boolean isCustomControllerTemplate() {
        return customControllerTemplate;
    }

    public void setCustomControllerTemplate(boolean customControllerTemplate) {
        this.customControllerTemplate = customControllerTemplate;
    }

    public String getCustomControllerTemplateName() {
        return customControllerTemplateName;
    }

    public void setCustomControllerTemplateName(String customControllerTemplateName) {
        this.customControllerTemplateName = customControllerTemplateName;
    }

    public boolean isCustomServiceTemplate() {
        return customServiceTemplate;
    }

    public void setCustomServiceTemplate(boolean customServiceTemplate) {
        this.customServiceTemplate = customServiceTemplate;
    }

    public String getCustomServiceTemplateName() {
        return customServiceTemplateName;
    }

    public void setCustomServiceTemplateName(String customServiceTemplateName) {
        this.customServiceTemplateName = customServiceTemplateName;
    }

    public boolean isCustomDomainTemplate() {
        return customDomainTemplate;
    }

    public void setCustomDomainTemplate(boolean customDomainTemplate) {
        this.customDomainTemplate = customDomainTemplate;
    }

    public String getCustomDomainTemplateName() {
        return customDomainTemplateName;
    }

    public void setCustomDomainTemplateName(String customDomainTemplateName) {
        this.customDomainTemplateName = customDomainTemplateName;
    }

    public boolean isCustomEntityTemplate() {
        return customEntityTemplate;
    }

    public void setCustomEntityTemplate(boolean customEntityTemplate) {
        this.customEntityTemplate = customEntityTemplate;
    }

    public String getCustomEntityTemplateName() {
        return customEntityTemplateName;
    }

    public void setCustomEntityTemplateName(String customEntityTemplateName) {
        this.customEntityTemplateName = customEntityTemplateName;
    }

    public boolean isCustomDaoTemplate() {
        return customDaoTemplate;
    }

    public void setCustomDaoTemplate(boolean customDaoTemplate) {
        this.customDaoTemplate = customDaoTemplate;
    }

    public String getCustomDaoTemplateName() {
        return customDaoTemplateName;
    }

    public void setCustomDaoTemplateName(String customDaoTemplateName) {
        this.customDaoTemplateName = customDaoTemplateName;
    }

    public boolean isWithCreateInfo() {
        return withCreateInfo;
    }

    public void setWithCreateInfo(boolean withCreateInfo) {
        this.withCreateInfo = withCreateInfo;
    }

    public List<TableMetaData> getTableMetaDataList() {
        return tableMetaDataList;
    }

    public void setTableMetaDataList(List<TableMetaData> tableMetaDataList) {
        this.tableMetaDataList = tableMetaDataList;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }

    /**
     * 获取模型Entity需要导入的类
     *
     * @return Set
     */
    public Set<String> getEntityImports() {
        Set<String> importSets = new HashSet<>();
        if (this.tableMetaDataList == null || this.tableMetaDataList.isEmpty()) {
            return importSets;
        }
        for (TableMetaData tableMetaData : this.tableMetaDataList) {
            String clzName = tableMetaData.getJavaTypeClass().getName();
            if (!clzName.startsWith("java.lang")) {
                importSets.add(clzName);
            }
        }
        return importSets;
    }

    /**
     * 获取文件(类)名称
     *
     * @param type          文件类型
     * @param needFileSufix 是否需要文件后缀
     * @return String
     */
    public String getCodeFileTypeName(Templates type, boolean needFileSufix) {
        StringBuilder builder = new StringBuilder();
        if (!type.isMapper() && StringUtils.isNotBlank(subPackage)) {
            builder.append(subPackage).append(".");
        }
        builder.append(getPrefix()).append(type.getSuffix());
        if (needFileSufix) {
            builder.append(type.getFileSuffix());
        }
        return builder.toString();
    }
}
